home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 354 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Observations on templates
  5. Date: 12 Feb 1996 10:22:39 PST
  6. Organization: Comp Sci, University of Melbourne
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4fnp8i$8e2@mulga.cs.mu.OZ.AU>
  9. References: <ACVI83na99@qsar.chem.msu.su> <4fa6d0$115g@news.gate.net>     <4fl2cn$hue@mulga.cs.mu.OZ.AU> <xsog2cgbnjx.fsf@juicer.cs.rpi.edu>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 12 Feb 1996 16:18:25 GMT
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMR+FfEy4NqrwXLNJAQHtfQIAmZeoC5ba2pfg4wcFNmZchxIVIBDmTmSC
  14.     VL9X6kCi4CR8M9pWVigk8O1Y5uZmKkwsCO1CVmUXFDOLvJdAEszIuw==
  15.     =ffMG
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. vandevod@cs.rpi.edu (David Vandevoorde) writes:
  19.  
  20. >>>>>> "FH" == Fergus Henderson <fjh@munta.cs.mu.OZ.AU> writes:
  21. >[...]
  22. >FH> template <class T, class S> T cast_d(S &s)     
  23. >    { return dynamic_cast<T>s; }
  24. >FH> template <class T, class S> T cast_d(const S &s) 
  25. >    { return dynamic_cast<T>s; }
  26. >FH> template <class T, class S> T cast_d(volatile S &s)
  27. >FH>    { return dynamic_cast<T>s; }
  28. >FH> template <class T, class S> T cast_d(const volatile S &s)
  29. >FH>     { return dynamic_cast<T>s; }
  30. >
  31. >Isn't the last one sufficient?
  32.  
  33. No, because 5.2.6[expr.cast.dynamic]/1 states that "dynamic_cast shall
  34. not cast away constness".  If you only defined the last one, then code
  35. such as
  36.  
  37.     class Foo : public Bar { /* ... */ };
  38.     void f(Bar& x) {
  39.         cast_d<Foo&>(x);
  40.     }
  41.  
  42. would be ill-formed, because it would instantiate
  43.  
  44.     cast_d<Foo&, Bar> Foo& cast_d(const volatile Bar &s)
  45.     { return dynaic_cast<Foo&>(s); }
  46.  
  47. and this dynamic_cast would be casting away constness.
  48.  
  49. Incidentally, 5.2.6 would seem to imply that dynamic_cast *is* allowed
  50. to cast away volatileness, but I think that is a mistake in the current
  51. draft.
  52.  
  53. >(and aren't parentheses required in `dynamic_cast<T>(s)'?).
  54.  
  55. Yes, they are -- thanks for correcting that error.
  56.  
  57. --
  58. Fergus Henderson                 WWW: http://www.cs.mu.oz.au/~fjh
  59. fjh@cs.mu.oz.au                  PGP: finger fjh@128.250.37.3
  60. ---
  61. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  62.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  63.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  64.